package com.yjps.system.service;

import com.yjps.common.page.Pager;
import com.yjps.system.dao.RecruitmentDao;
import com.yjps.system.model.Recruitment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 麒全 on 2015/8/18.
 */
@Service
public class RecruitmentService {
    @Autowired
    RecruitmentDao recruitmentDao;

    private static final Logger log = LoggerFactory.getLogger(RecruitmentService.class);


    public List<Recruitment> getList() {
        String hql = "from Recruitment r";

        return recruitmentDao.find(hql);
    }

    /**
     * 获得分页招聘信息
     * @param pager
     * @return
     */
    public Pager<Recruitment> getRecruitmentByPager(Pager<Recruitment> pager){
        Map<String, Object> params = new HashMap<>();
        String queryString = "from Recruitment t where t.isValid = 1";
        queryString += " order by t.id desc";
        List<Recruitment> accounts = recruitmentDao.findWithSelect(queryString, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
        pager.setCount(recruitmentDao.getTotalCount(queryString, params));
        pager.setList(accounts);
        return pager;
    }

    public Recruitment find(int id) {
        String hql = "from Recruitment r where r.id = :id";
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return (Recruitment)recruitmentDao.findUniqueResult(hql, map);

    }

    public void add(Recruitment r) {
        recruitmentDao.save(r);
    }

    public Recruitment update(Recruitment r) {
        /*recruitmentDao.update(r);*/
        String hql = "update Recruitment r set r.position = :position , r.request = :request," +
                "r.treatment = :treatment,r.state =:state,r.responsibility =:responsibility,r.positionType =:positionType," +
                "r.num =:num,r.workPosition =:workPosition,r.announceTime =:announceTime," +
                "r.deadline =:deadline,r.jobAttribute =:jobAttribute,r.department =:department,r.phone =:phone " +
                " where r.id = :id";
        Map<String,Object> params = new HashMap<>();
        params.put("position",r.getPosition());
        params.put("request",r.getRequest());
        params.put("treatment",r.getTreatment());
        params.put("state",r.getState());
        params.put("responsibility",r.getResponsibility());
        params.put("positionType",r.getPositionType());
        params.put("num",r.getNum());
        params.put("workPosition",r.getWorkPosition());
        params.put("announceTime",r.getAnnounceTime());
        params.put("deadline",r.getDeadline());
        params.put("jobAttribute",r.getJobAttribute());
        params.put("department",r.getDepartment());
        params.put("phone",r.getPhone());
        params.put("id",r.getId());
        recruitmentDao.excute(hql, params);
        log.debug("update the Recruitment success, id is " + r.getId());
        return recruitmentDao.load(r.getId());
    }

    public void delete(Recruitment r) {
        recruitmentDao.invalid(r);
        log.debug("delete the Recruitment success, id is " + r.getId());

    }
}
